import 'package:bruno/bruno.dart';
import 'package:dart_date/dart_date.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/constants/time_formated.dart';

class TimeRangePicker extends StatefulWidget {
  final String? title;
  final TextStyle? titleStyle;
  String? startTime;
  String? endTime;
  EdgeInsets? padding;
  String? startPlaceholder;
  String? endPlaceholder;
  void Function(String start, String end)? onChange;

  TimeRangePicker({
    super.key,
    this.title,
    this.titleStyle,
    this.startTime,
    this.endTime,
    this.padding,
    this.onChange,
    this.startPlaceholder,
    this.endPlaceholder,
  });

  @override
  State<TimeRangePicker> createState() => _TimeRangePickerState();
}

class _TimeRangePickerState extends State<TimeRangePicker> {
  DateTime get now => DateTime.now();

  @override
  void initState() {
    super.initState();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        showDatePicker();
      },
      child: Container(
        padding: widget.padding,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            if (widget.title != null)
              Text(
                widget.title!,
                style: TextStyles.f14.boldStyle.merge(widget.titleStyle),
              ),
            SizedBox(
              height: 8.h,
            ),
            SizedBox(
              height: 38.h,
              child: Row(
                children: [
                  Expanded(
                    child: timeTextWidget(
                      widget.startTime,
                      widget.startPlaceholder,
                    ),
                  ),
                  Text(
                    ResKey.to.tr,
                    style: TextStyles.f14,
                  ),
                  Expanded(
                    child: timeTextWidget(
                      widget.endTime,
                      widget.endPlaceholder,
                    ),
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  Container timeTextWidget(String? time, String? placeholder) {
    return Container(
      alignment: Alignment.center,
      decoration: const BoxDecoration(
        border: Border(
          bottom: BorderSide(color: Colour.dividerColor),
        ),
      ),
      child: time == null || time.isEmpty
          ? Text(
              placeholder ?? ResKey.pleSelect.tr,
              style: TextStyles.lf14,
            )
          : Text(time, style: TextStyles.f14),
    );
  }

  showDatePicker() {
    String format = 'yyyy-MM-dd';
    DateTime? startDateTime =
        (widget.startTime == null || widget.startTime!.isEmpty)
            ? now
            : DateTime.tryParse(widget.startTime!);
    DateTime? endDateTime =
        (widget.startTime == null || widget.startTime!.isEmpty)
            ? now
            : DateTime.tryParse(widget.endTime!);
    BrnPickerTitleConfig pickerTitleConfig =
        const BrnPickerTitleConfig(titleContent: "");
    BrnDateRangePicker.showDatePicker(
      context,
      isDismissible: false,
      pickerMode: BrnDateTimeRangePickerMode.date,
      minuteDivider: 10,
      pickerTitleConfig: pickerTitleConfig,
      dateFormat: format,
      initialStartDateTime: startDateTime,
      initialEndDateTime: endDateTime,
      onConfirm: (startDateTime, endDateTime, startlist, endlist) {
        widget.startTime = startDateTime.format(TimeFormated.dateShowFromated);
        widget.endTime = endDateTime.format(TimeFormated.dateShowFromated);
        setState(() {});
        widget.onChange?.call(widget.startTime!, widget.endTime!);
      },
    );
  }
}
